/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SetWorkSpace.java
 *
 * Created on Jan 14, 2011, 2:44:30 PM
 */

package workSpace;

import java.io.File;
import java.awt.Point;
import java.util.logging.Level;
import javax.swing.JFileChooser;

/**
 *
 * @author leng
 */
public class SetWorkSpace extends javax.swing.JFrame {

    protected userInterface.GUI gui;
    protected String callfromID=null;
    protected java.awt.Point location = null;
    protected java.awt.Dimension __size = null;
    protected boolean OKButtonClicked = false;

    boolean health_of_workspace = false;

    /** Creates new form SetWorkSpace */
    public SetWorkSpace(userInterface.GUI _gui) {
        gui = _gui;
        callfromID = null;
        initComponents();
        jTextField1.setText(null);
        //this.setModalExclusionType(ModalExclusionType.APPLICATION_EXCLUDE);

        // designate the screen position for current dialog
        setFramePosition();
        OKButtonClicked = false;

        // while this dialog are launched by eHOST automatically to force
        // user to choose a folder as the workspace of eHOST, we hide 
        // the "cancel" button.
        if (this.callfromID == null){
            jButton2.setVisible(false);
        }
        
    }


    public SetWorkSpace(userInterface.GUI _gui, String callfromID, Point _location,
            java.awt.Dimension _size)
    {
        gui = _gui;
        this.callfromID = callfromID;
        this.location = _location;
        this.__size = _size;
        initComponents();
        jTextField1.setText(env.Parameters.WorkSpace.WorkSpace_AbsolutelyPath);

        //this.setModalExclusionType(ModalExclusionType.APPLICATION_EXCLUDE);

        // designate the screen position for current dialog
        setFramePosition();
        
        // while this dialog are launched by eHOST automatically to force
        // user to choose a folder as the workspace of eHOST, we hide 
        // the "cancel" button.
        if (this.callfromID == null){
            jButton2.setVisible(false);
        }
    }


    /**Set screen location of this dialog. It has two different screen location,
     * first, this dialog will be put on the exact middle of current eHOST main
     * window while it called in eHOST's initinal process; Second situation,
     * this dialog will be put on the middle of the right work space just while
     * it called by the NAV pane.
     * 
     */
    private void setFramePosition(){

        if(this.callfromID!=null){
            this.jButton4.setVisible(false);
        }

        int width = 675, height = 276;

        //##1## dialog called from the NAV pane, set this dialog in the middle
        // of right area
        if(callfromID!=null){
            if((__size==null)||(location==null)) {
                log.LoggingToFile.log( Level.SEVERE, "error 1102030529:: fail to import jcomponents.");
                return;
            }
                        
            
            int x1 = location.x + (int)((__size.width - width)/2);
            int y1 = location.y + (int)((__size.height - height)/2);
            this.setLocation(x1, y1);

            return;
        }
        
        //##2## set the dialog in the middle of whole area
        else
        {
            
            int guix = gui.getX(), guiy = gui.getY();
            int guiWidth = gui.getWidth(), guiHeight = gui.getHeight();            
            int x = guix + (int)((guiWidth - width)/2);
            int y = guiy + (int)((guiHeight - height)/2);
            this.setLocation(x, y);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jButton3 = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Workspace Launcher");
        setAlwaysOnTop(true);
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Cambria", 0, 14));
        jLabel3.setText("Workspace:");

        jButton3.setText("Browse..");
        jButton3.setPreferredSize(new java.awt.Dimension(100, 24));
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel3)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jTextField1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 480, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(jButton3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel3)
                    .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jButton3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(129, Short.MAX_VALUE))
        );

        getContentPane().add(jPanel1, java.awt.BorderLayout.CENTER);

        jPanel2.setBackground(new java.awt.Color(253, 253, 253));
        jPanel2.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(153, 153, 153)));

        jLabel1.setFont(new java.awt.Font("Cambria", 1, 14));
        jLabel1.setText("Select a workspace");

        jLabel2.setFont(new java.awt.Font("Cambria", 0, 14)); // NOI18N
        jLabel2.setText("Choose a workspace folder to save your annotation projects.");

        org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLabel1)
                    .add(jLabel2))
                .addContainerGap(303, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .add(9, 9, 9)
                .add(jLabel1)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jLabel2)
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        getContentPane().add(jPanel2, java.awt.BorderLayout.PAGE_START);

        jButton1.setText("OK");
        jButton1.setPreferredSize(new java.awt.Dimension(100, 24));
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Cancel");
        jButton2.setPreferredSize(new java.awt.Dimension(100, 24));
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton4.setText("Quit");
        jButton4.setPreferredSize(new java.awt.Dimension(100, 24));
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel3Layout = new org.jdesktop.layout.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel3Layout.createSequentialGroup()
                .add(jButton4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 369, Short.MAX_VALUE)
                .add(jButton2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jButton1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel3Layout.createSequentialGroup()
                .add(jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jButton1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jButton2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jButton4, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(24, Short.MAX_VALUE))
        );

        getContentPane().add(jPanel3, java.awt.BorderLayout.PAGE_END);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        OKButtonClicked = false;
        this.dispose();
    }//GEN-LAST:event_jButton2ActionPerformed

    /**User clicked the "close" button after selecting a path for eHOST 
     * workspace. 
     * 
     * @param  evt  The mouse action event that user clicking on the "close"
     *          button.
     */
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
              
        OKButtonClicked = true;
        env.Parameters.WorkSpace.WorkSpace_AbsolutelyPath = this.jTextField1.getText();
        health_of_workspace = false;

            String path = env.Parameters.WorkSpace.WorkSpace_AbsolutelyPath;
            if( (path==null) || (path.trim().length()<1) )
            {
                commons.Tools.beep();
                health_of_workspace = false;
                this.dispose();
                return;
            }

            File pathfile = new File(path);
            if((pathfile==null)||(!pathfile.exists())||(pathfile.isFile()))
            {
                commons.Tools.beep();
                health_of_workspace = false;
                this.dispose();
                return;
            }

            env.Parameters.WorkSpace.CurrentProject = pathfile;
            health_of_workspace = true;

        

            gui.display_hideEditor();  
            // make sure current workspace is displayed on the combobox
            //gui.setCurrentWorkspace_selected(env.Parameters.WorkSpace.CurrentProject);
            this.dispose();
        
    }//GEN-LAST:event_jButton1ActionPerformed

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
        // hide the document viewer after switch a new workspace
        gui.display_document_viewer(false);

        try{
            gui.setEnabled(true);

            //gui.reavtiveMainPanel();

            // ### 1 ### if current dialog to select workspace is auto launched
            // by the eHOST
            if (this.callfromID == null)
            {
                gui.checkWorkSpaceDialog();
            }
            // ### 2 ### if this dialog launched by user's clicking
            else
            {
                if(!OKButtonClicked)
                    return;
                
                // if
                if(this.health_of_workspace){
                    gui.display_hideEditor();
                    String path = env.Parameters.WorkSpace.WorkSpace_AbsolutelyPath;
                    env.Parameters.WorkSpace.CurrentProject = new File(path);
                    gui.refreshNAVProjects();
                    gui.setCurrentWorkspace_selected(new File(path));
                    gui.display_hideEditor();
                }else{
                    gui.display_hideEditor();
                    gui.dialog_toInputNewWorkspacePath();
                    gui.display_hideEditor();
                }

            }
        }catch(Exception ex){
            log.LoggingToFile.log( Level.SEVERE, "### ERROR ### 1106151500:: fail to change workspace!!!\n"
                    + ex.getMessage() );
        }



        

        
    }//GEN-LAST:event_formWindowClosed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        File dir = openFileDialog();
        if (dir != null)
            jTextField1.setText(dir.getAbsolutePath());

    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        System.exit(-99);
    }//GEN-LAST:event_jButton4ActionPerformed


    /**pop up a new dialog to let user  */
    private File openFileDialog()
    {
        String latestUsedFolder = env.Parameters.WorkSpace.WorkSpace_AbsolutelyPath;
        File latestUsedDirectoryOfWorkSpace = null;
        if( latestUsedFolder != null )
             latestUsedDirectoryOfWorkSpace= new File(latestUsedFolder);
        JFileChooser filechooser = new JFileChooser();
        if (latestUsedFolder!=null)
            filechooser.setCurrentDirectory(latestUsedDirectoryOfWorkSpace);
        filechooser.setMultiSelectionEnabled(false);
        filechooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);        
        filechooser.setAcceptAllFileFilterUsed(false);

        int result = filechooser.showOpenDialog(this);

        if (filechooser.getSelectedFile() != null
                && result == JFileChooser.APPROVE_OPTION) {
            return filechooser.getSelectedFile();
        }
       
        filechooser.resetChoosableFileFilters();

        return null;
    }
    /*
     
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new SetWorkSpace().setVisible(true);
            }
        });
    }
    */

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables

}
